<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <title>Logo — Space.js</title>

    <link rel="preconnect" href="https://fonts.gstatic.com">
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Roboto+Mono">
    <link rel="stylesheet" href="assets/css/style.css">

    <script type="module">
        import { Interface, ticker } from '../src/index.js';

        class Logo extends Interface {
            constructor() {
                super('.logo');

                this.initHTML();

                this.addListeners();
                this.onResize();
            }

            initHTML() {
                this.css({
                    position: 'absolute',
                    left: 50,
                    top: 50,
                    width: 64,
                    height: 64,
                    cursor: 'pointer',
                    webkitUserSelect: 'none',
                    userSelect: 'none',
                    opacity: 0
                });

                this.image = new Interface(null, 'img');
                this.image.attr({ src: 'assets/images/alienkitty.svg' });
                this.image.css({
                    width: '100%',
                    height: 'auto'
                });
                this.add(this.image);
            }

            addListeners() {
                window.addEventListener('resize', this.onResize);
                this.element.addEventListener('mouseenter', this.onHover);
                this.element.addEventListener('mouseleave', this.onHover);
                this.element.addEventListener('click', this.onClick);
            }

            removeListeners() {
                window.removeEventListener('resize', this.onResize);
                this.element.removeEventListener('mouseenter', this.onHover);
                this.element.removeEventListener('mouseleave', this.onHover);
                this.element.removeEventListener('click', this.onClick);
            }

            // Event handlers

            onResize = () => {
                const width = document.documentElement.clientWidth;
                const height = document.documentElement.clientHeight;

                if (width < height) {
                    this.css({
                        left: 30,
                        top: 30,
                        width: 40,
                        height: 40
                    });
                } else {
                    this.css({
                        left: 50,
                        top: 50,
                        width: 64,
                        height: 64
                    });
                }
            };

            onHover = ({ type }) => {
                this.clearTween();

                if (type === 'mouseenter') {
                    this.tween({ opacity: 0.6 }, 300, 'easeOutCubic');
                } else {
                    this.tween({ opacity: 1 }, 300, 'easeOutCubic');
                }
            };

            onClick = () => {
                open('https://alien.js.org/');
            };

            // Public methods

            animateIn = () => {
                this.tween({ opacity: 1 }, 600, 'easeInOutSine');
            };

            destroy = () => {
                this.removeListeners();

                return super.destroy();
            };
        }

        class App {
            static async init() {
                this.initViews();

                this.addListeners();
            }

            static initViews() {
                this.logo = new Logo();
                document.body.appendChild(this.logo.element);
            }

            static addListeners() {
                window.addEventListener('load', this.onLoad);
                ticker.start();
            }

            // Event handlers

            static onLoad = () => {
                this.logo.animateIn();
            };
        }

        App.init();
    </script>
</head>
<body>
</body>
</html>
